package other.xsy.passlys.receivable;

import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEvent;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventRequest;
import com.rkhd.platform.sdk.creekflow.approvalevent.ApprovalEventResponse;
import com.rkhd.platform.sdk.data.model.InvoiceDetail__c;
import com.rkhd.platform.sdk.data.model.Invoice__c;
import com.rkhd.platform.sdk.data.model.ScarletDetail__c;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import other.xsy.passlys.util.HCXObjectService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 开票审批通过回写booking、应收
 * @Author 黎炎松
 * 2024-08-07 14:13
 */
public class InvoiceRedCompleteAfter implements ApprovalEvent {

    private static HCXObjectService hcxObjectService = new HCXObjectService();
    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "开票红冲审批通过 ==> ";


    public static void main(String[] args) {
        InvoiceRedCompleteAfter invoiceRedCompleteAfter = new InvoiceRedCompleteAfter();
        ApprovalEventRequest approvalEventRequest = new ApprovalEventRequest();
        approvalEventRequest.setDataId(3420611435125377L);
        invoiceRedCompleteAfter.execute(approvalEventRequest);
    }

    @Override
    public ApprovalEventResponse execute(ApprovalEventRequest request) {
        logger.info(LOGGER_MSG + "触发开始");
        logger.info("ObjectApiKey:" + request.getEntityApiKey() + " , DataId:" + request.getDataId());
        try {
            Long redInvoiceId = request.getDataId();

            // 查询开票红冲下的红冲明细数据,获取 invoiceIds
            String sql = "select id,invoice__c from scarletDetail__c where redInvoice__c = " + redInvoiceId;
            List<ScarletDetail__c> scarletDetailList = hcxObjectService.query(sql, ScarletDetail__c.class);

            // 修改当前发票红冲下的开票状态为作废
            List<Invoice__c> upInvoiceList = new ArrayList<>();
            for (ScarletDetail__c scarletDetail : scarletDetailList) {
                Invoice__c invoice = new Invoice__c();
                invoice.setId(scarletDetail.getInvoice__c());
                invoice.setCustomItem14__c(3);
                upInvoiceList.add(invoice);
            }
            Boolean update = hcxObjectService.batchUpdate(upInvoiceList);
            logger.info(LOGGER_MSG + "批量修改开票状态:" + update);


            // 根据开票，获取开票下的 bookingIds
            String invoiceIds = scarletDetailList.stream().map(v -> v.getInvoice__c().toString()).collect(Collectors.joining(","));
            sql = String.format("select id,booking__c from invoiceDetail__c where invoice__c in (%s)", invoiceIds);
            List<InvoiceDetail__c> invoiceDetailList = hcxObjectService.query(sql, InvoiceDetail__c.class);
            List<Long> bookingIds = invoiceDetailList.stream().map(v -> v.getBooking__c()).collect(Collectors.toList());
            logger.info(LOGGER_MSG + "bookingIds:" + bookingIds);

            // 延迟2s (xoql有延迟问题)
            Thread.sleep(2000);

            // 修改开票金额字段
            ReceivableService receivableService = new ReceivableService();
            receivableService.calcInvoiceAmounByBooking(bookingIds);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        ApprovalEventResponse response = new ApprovalEventResponse();
        response.setSuccess(true);
        response.setMsg("success");
        return response;
    }

}
